﻿using Shop.Core.Repository;
using Shop.Core.Share;
using Shop.IServices.Admin;
using Shop.Model.Dto.Admin.Gift;
using Shop.Model.Entity.Goo;
using Shop.Model.View.Admin.Gift;
using SqlSugar;

namespace Shop.Services.Admin;

public class GiftService : AbstractService, IGiftService
{
    private readonly RepositoryBase<GoodsGift> _giftRepository;
    private readonly RepositoryBase<GoodsGoods> _godsRepository;
    private readonly RepositoryBase<GoodsSize> _sizeRepository;

    public GiftService(RepositoryBase<GoodsGift> giftRepository,
        RepositoryBase<GoodsGoods> godsRepository,
        RepositoryBase<GoodsSize> sizeRepository)
    {
        _giftRepository = giftRepository;
        _godsRepository = godsRepository;
        _sizeRepository = sizeRepository;
    }

    /// <summary>
    /// 赠品活动列表
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public AppResult<PageModel<GiftListVo>> GiftList(GiftListDto dto)
    {
        var records = 0;
        var data = _giftRepository.Queryable<GoodsGift, GoodsGoods, GoodsSize>((t1, t2, t3) => new object[]
            {
                JoinType.Left, t1.GoodsId == t2.Id,
                JoinType.Left, t1.GoodsSpecId == t3.Id,
            })
            .WhereIF(!string.IsNullOrEmpty(dto.GName), (t1, t2, t3) => t2.Name.Contains(dto.GName))
            .WhereIF(dto.CreateStartTime.HasValue, (t1, t2, t3) => t1.CreateTime >= dto.CreateStartTime.Value)
            .WhereIF(dto.CreateEndTime.HasValue, (t1, t2, t3) => t1.CreateTime < dto.CreateEndTime.Value)
            .WhereIF(dto.DownStartTime.HasValue, (t1, t2, t3) => t1.DownTime >= dto.DownStartTime.Value)
            .WhereIF(dto.DownEndTime.HasValue, (t1, t2, t3) => t1.DownTime < dto.DownEndTime.Value)
            .WhereIF(dto.UpStartTime.HasValue, (t1, t2, t3) => t1.UpTime >= dto.UpStartTime.Value)
            .WhereIF(dto.UpEndTime.HasValue, (t1, t2, t3) => t1.UpTime < dto.UpEndTime.Value)
            .WhereIF(dto.IsDel.HasValue, (t1, t2, t3) => t1.IsDel == dto.IsDel.Value)
            .OrderBy("t1.CreateTime DESC")
            .Select((t1, t2, t3) => new GiftListVo()
            {
                Id = t1.Id,
                GName = t2.Name,
                SName = t3.Name,
                BuyCount = t1.BuyCount,
                GiveCount = t1.GiveCount,
                IsSuperposition = t1.IsSuperposition,
                IsLimited = t1.IsLimited,
                RemainderCount = t1.RemainderCount,
                UpTime = t1.UpTime,
                DownTime = t1.DownTime,
                CreateTime = t1.CreateTime,
                IsDel = t1.IsDel
            })
            .ToPageList(dto.PageIndex, dto.PageSize, ref records);
        return new PageModel<GiftListVo>(dto, data, records);
    }

    /// <summary>
    /// 添加赠品活动
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public AppResult AddGift(AddGiftDto dto)
    {
        var godInfo = _godsRepository.Where(x => x.Id == dto.GoodsId).First();
        if (godInfo == null)
            return Problem("商品不存在");
        var godSizeInfo = _sizeRepository.Where(x => x.Id == dto.GoodsSpecId).First();
        if (godSizeInfo == null)
            return Problem("商品规格不存在");
        var giftGoodInfo = _godsRepository.Where(x => x.Id == dto.GiftId).First();
        if (giftGoodInfo == null)
            return Problem("赠品不存在");
        var giftSizeInfo = _sizeRepository.Where(x => x.Id == dto.GiftSpecId).First();
        if (giftSizeInfo == null)
            return Problem("赠品规格不存在");
        if (dto.IsLimited)
        {
            if (dto.GiftTotal <= 0)
                return Problem("赠品总数不可小于1");
        }

        var giftInfo = _giftRepository
            .Where(x => x.IsDel == false && x.GoodsId == godInfo.Id && x.GoodsSpecId == godSizeInfo.Id).First();
        if (giftInfo != null)
            return Problem("请关闭此商品的上一个活动");
        var giftVo = new GoodsGift()
        {
            GoodsId = dto.GoodsId,
            GoodsSpecId = dto.GoodsSpecId,
            GiftId = dto.GiftId,
            GiftSpecId = dto.GiftSpecId,
            BuyCount = dto.BuyCount,
            GiveCount = dto.GiveCount,
            IsSuperposition = dto.IsSuperposition,
            IsLimited = dto.IsLimited,
            IsDel = dto.IsDel
        };
        if (dto.IsLimited)
        {
            giftVo.GiftTotal = dto.GiftTotal;
            giftVo.RemainderCount = dto.GiftTotal;
        }

        if (dto.UpTime.HasValue)
            giftVo.UpTime = dto.UpTime.Value;

        if (dto.DownTime.HasValue)
            giftVo.DownTime = dto.DownTime.Value;

        _giftRepository.Insert(giftVo);
        return new AppResult();
    }

    /// <summary>
    /// 赠品活动详情
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public AppResult<GiftInfoVo> GiftInfo(long dto)
    {
        var data = _giftRepository.Where(x => x.Id == dto)
            .Select(x => new GiftInfoVo()
            {
                Id = x.Id,
                GoodsId = x.GoodsId,
                GoodsSpecId = x.GoodsSpecId,
                GiftId = x.GiftId,
                GiftSpecId = x.GiftSpecId,
                BuyCount = x.BuyCount,
                GiveCount = x.GiveCount,
                IsSuperposition = x.IsSuperposition,
                IsLimited = x.IsLimited,
                GiftTotal = x.GiftTotal,
                RemainderCount = x.RemainderCount,
                UpTime = x.UpTime,
                DownTime = x.DownTime,
                IsDel = x.IsDel
            })
            .First();
        if (data == null)
            return Problem("赠品活动不存在");
        return data;
    }

    /// <summary>
    /// 结束赠品活动
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public AppResult GiftDown(long dto)
    {
        var data = _giftRepository.Where(x => x.Id == dto).First();
        if (data == null)
            return Problem("赠品活动不存在");
        data.IsDel = true;
        _giftRepository.Update(data);
        return new AppResult();
    }

    /// <summary>
    /// 编辑赠品活动
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public AppResult GiftEdit(GiftEditDto dto)
    {
        var data = _giftRepository.Where(x => x.Id == dto.Id).First();
        if (data == null)
            return Problem("赠品活动不存在");
        var godInfo = _godsRepository.Where(x => x.Id == dto.GoodsId).First();
        if (godInfo == null)
            return Problem("商品不存在");
        var godSizeInfo = _sizeRepository.Where(x => x.Id == dto.GoodsSpecId).First();
        if (godSizeInfo == null)
            return Problem("商品规格不存在");
        var giftGoodInfo = _godsRepository.Where(x => x.Id == dto.GiftId).First();
        if (giftGoodInfo == null)
            return Problem("赠品不存在");
        var giftSizeInfo = _sizeRepository.Where(x => x.Id == dto.GiftSpecId).First();
        if (giftSizeInfo == null)
            return Problem("赠品规格不存在");
        if (dto.IsLimited)
        {
            if (dto.GiftTotal <= 0)
                return Problem("赠品总数不可小于1");
            if (data.IsLimited)
            {
                if (dto.GiftTotal < data.GiftTotal)
                {
                    if (dto.GiftTotal < (data.GiftTotal - data.RemainderCount))
                        return Problem("赠送数量以大于赠送总数，如要结束请删除活动");
                }
            }
        }

        data.Id = dto.Id;
        data.GoodsId = dto.GoodsId;
        data.GoodsSpecId = dto.GoodsSpecId;
        data.GiftId = dto.GiftId;
        data.GiftSpecId = dto.GiftSpecId;
        data.BuyCount = dto.BuyCount;
        data.GiveCount = dto.GiveCount;
        data.IsSuperposition = dto.IsSuperposition;
        data.IsLimited = dto.IsLimited;
        data.IsDel = dto.IsDel;
        
        // 新参数是否有赠送数量限制
        if (dto.IsLimited)
        {
            // 旧参数是否有赠送数量限制
            if (data.IsLimited)
            {
                // 新参数<旧参数,剩余赠送数量为：赠送数量 - (旧参数 - 新参数)
                //新参数>=旧参数,剩余赠送数量为：赠送数量 + (新参数 - 旧新参数)
                if (dto.GiftTotal < data.GiftTotal)
                    data.RemainderCount -= (data.GiftTotal - dto.GiftTotal);
                else
                    data.RemainderCount += (dto.GiftTotal - data.GiftTotal);
            }
            else
                data.RemainderCount = dto.GiftTotal;
            data.GiftTotal = dto.GiftTotal;
        }
        else
        {
            data.GiftTotal = 0;
            data.RemainderCount = 0;
        }

        if (dto.UpTime.HasValue)
            data.UpTime = dto.UpTime.Value;

        if (dto.DownTime.HasValue)
            data.DownTime = dto.DownTime.Value;

        _giftRepository.Update(data);
        return new AppResult();
    }
}